#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/6 21:53
# @Author : linjinting
# @Project : tableview-model
# @File : gzpw_ticket.py
# @Software: PyCharm

import sys
# from PySide2.QtWidgets import (QApplication, QWidget, QSizePolicy, QLabel)
from UI_gzpw_ticket import *
from checkable_comboBox import ComboCheckBox
from tablemodel import TableModel, SortProxyModel
from mysql_model.mysql_handle import TicketHandle
from unit.config import mysql_config


class Ticket(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Ticket, self).__init__(parent)
        self.setupUi(self)
        mysql_config['port'] = int(mysql_config['port'])
        self.m_ticket_handle = TicketHandle(mysql_config)

        # self.gridLayout_table_tool = QGridLayout(self.widget_table_tool)
        # self.gridLayout_table_tool.setObjectName(u"gridLayout_table_tool")
        label = QLabel(self.widget_table_filter)
        label.setText('列筛选：')
        self.column_checkable_comboBox = ComboCheckBox(self.widget_table_filter)
        sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.column_checkable_comboBox.sizePolicy().hasHeightForWidth())
        self.column_checkable_comboBox.setSizePolicy(sizePolicy)
        self.column_checkable_comboBox.setMinimumSize(QSize(200, 0))
        self.horizontalLayout.addWidget(label)
        self.horizontalLayout.addWidget(self.column_checkable_comboBox)
        self.column_checkable_comboBox.add_item("全选")

        self.bound_signal()

    def init_model(self, data):
        # 表格视图
        # data = [
        #     ['11', 'ceshi22', '20200711', '作废'],
        #     ['22', 'ceshi00', '20200711', '未完成'],
        #     ['33', 'ceshi11', '20200711', '已完成'],
        #     ['44', 'ceshi13', '20200711', '已完成'],
        #     ['55', 'ceshi17', '20200711', '已完成'],
        # ]
        # header = ['id', 'name', 'time']

        self.table_model = TableModel()
        self.table_model.initData(data)
        # 新建一个排序代理
        self.sort_proxy = SortProxyModel()
        # 设置代理的模型
        self.sort_proxy.setSourceModel(self.table_model)
        # 开启动态排序与过滤
        self.sort_proxy.setDynamicSortFilter(True)
        # 从第0行开始排序
        # self.sort_proxy.sort(0)

    def init_table_view(self):
        self.tableView_ticket_info.setModel(self.sort_proxy)
        self.tableView_ticket_info.resizeColumnsToContents()
        # count = self.table_model.columnCount()
        # self.tableView_ticket_info.horizontalHeader().setSectionResizeMode(self.table_model.columnCount() - 1,
        #                                                                    QHeaderView.Stretch)
        # self.tableView_ticket_info.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)

        # self.tableView_ticket_info.setSortingEnabled(True)
        # self.tableView_ticket_info.verticalHeader().set
        self.comboBox_table_filed.addItems(self.table_model.m_tableHeader)

        # column checkable
        self.column_checkable_comboBox.add_items(self.table_model.m_tableHeader)

    def bound_signal(self):
        self.pushButton_get_data.clicked.connect(self.get_sql_data)
        self.lineEdit_search_name.textChanged.connect(self.search_changed)
        self.tableView_ticket_info.horizontalHeader().sectionClicked.connect(self.horizontal_header_clicked)
        self.comboBox_table_filed.currentIndexChanged.connect(self.table_filed_changed)

        self.column_checkable_comboBox.currentIndexChanged.connect(self.filter_column)

    def search_changed(self, change_text):
        if self.sort_proxy is None:
            return
        # print(self.comboBox_table_filed.currentIndex())
        self.sort_proxy.set_filter(self.comboBox_table_filed.currentIndex(), change_text)

    def filter_column(self):
        # print('filter_column')
        self.sort_proxy.set_column_index_filter(self.column_checkable_comboBox.get_checked_item())

    def horizontal_header_clicked(self):
        self.tableView_ticket_info.setSortingEnabled(True)

    def table_filed_changed(self):
        self.sort_proxy.clean_filter()

    def get_sql_data(self):
        data = self.m_ticket_handle.get_history_ticket()
        self.init_model(data)
        self.init_table_view()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Ticket()
    window.show()
    sys.exit(app.exec_())
